#!/bin/bash
function backupSource(){
  # 定义备份文件名并初始化计数器
  backup_base="/etc/apt/sources.list.backup"
  max_backups=5 # 最大备份数

  # 确定下一个备份文件的编号
  # 如果文件计数达到max_backups，则从1开始循环覆盖
  for (( counter=1; counter<=max_backups; counter++ )); do
    backup_file="${backup_base}${counter}"
    if [ ! -f "$backup_file" ]; then
      break
    fi
    if [ $counter -eq $max_backups ]; then
      counter=1
      backup_file="${backup_base}${counter}"
      break
    fi
  done
  # 备份原来的sources.list文件
  sudo cp /etc/apt/sources.list $backup_file
  echo "备份原sources.list文件为$backup_file"
}

function changeSourceFile(){
  # 获取当前系统的代号
  source /etc/os-release
  VERSION_CODENAME=$VERSION_CODENAME
  # 显示选择菜单
  echo "请选择您要更换的源:"
  echo "1. 更换为清华源"
  echo "2. 更换为中科大源"
  read -p "请输入1或2: " choice

  # 根据选择更换源
  case $choice in
    1)
      echo "正在更换为清华源..."
      echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME main restricted universe multiverse
      deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME-security main restricted universe multiverse
      deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME-security main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME-backports main restricted universe multiverse
      deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME-backports main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME-proposed main restricted universe multiverse
      deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME-proposed main restricted universe multiverse
      deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME-updates main restricted universe multiverse
      deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ $VERSION_CODENAME-updates main restricted universe multiverse" | sudo tee /etc/apt/sources.list
      ;;
    2)
      echo "正在更换为中科大源..."
      echo "deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-security main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-security main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-backports main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-backports main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-proposed main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-proposed main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-updates main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-updates main restricted universe multiverse" | sudo tee /etc/apt/sources.list
      ;;
    *)
      echo "正在更换为中科大源..."
      echo "deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-security main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-security main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-backports main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-backports main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-proposed main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-proposed main restricted universe multiverse
      deb https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-updates main restricted universe multiverse
      deb-src https://mirrors.ustc.edu.cn/ubuntu/ $VERSION_CODENAME-updates main restricted universe multiverse" | sudo tee /etc/apt/sources.list
      ;;
  esac
  sudo apt update
}

function main(){
  changeSourceFile
}
#action
main $@